package com.example.dell.fastshopping.product;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.dell.fastshopping.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ${YSH} on 2019/5/28.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class ProductLeftFragment extends Fragment {

    @BindView(R.id.rv)
    RecyclerView rv;
    private Unbinder bind;
    private ProductModel model;
    private List<ProductMenu> mList;
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_product_left, container, false);
        bind = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        model = ViewModelProviders.of(getActivity()).get(ProductModel.class);
        initView();
    }

    private void initView() {
        model.getProductType().observe(this, new Observer<List<ProductMenu>>() {
            @Override
            public void onChanged(@Nullable List<ProductMenu> list) {
                mList = list;
                setData();
            }
        });
    }

    private void setData() {
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.setAdapter(new MyAdapter());
        if (mList.size() > 0) {
            setSelect(0);
        }
    }

    private void setSelect(int position) {
        for (ProductMenu p : mList) {
            p.setSelect(false);
        }
        model.setSelectValue(mList.get(position));
        mList.get(position).setSelect(true);
        rv.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {
        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_product_left, viewGroup, false);
            MyHolder myHolder = new MyHolder(inflate);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            ProductMenu productMenu = mList.get(i);
            if (productMenu.isSelect()) {
                myHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.white));
                myHolder.select.setVisibility(View.VISIBLE);
            } else {
                myHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.pager_grey));
                myHolder.select.setVisibility(View.GONE);
            }

            myHolder.content.setText(productMenu.getLabel());
            myHolder.setPosition(i);
            myHolder.itemView.setOnClickListener(myHolder);
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    private class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public View select;
        public TextView content;

        private int position;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            select = itemView.findViewById(R.id.select_img);
            content = itemView.findViewById(R.id.content);
        }

        @Override
        public void onClick(View v) {
            setSelect(position);
        }

        public void setPosition(int i) {
            this.position = i;
        }
    }
}
